<html>
<head></head>
<body>

<p>Test bug 13141 : XMLHttpRequest should set readyState to 0 after abort() </p>
<p>Updated for bug 16989: Add send() flag checks in XmlHttpRequest </p>
<p>Should see "PASS (reasyState event send)" & "PASS" (in that order) three times:</p>
<div id="ans"></div>

<script type="text/javascript">
function log(message) {
    document.getElementById("ans").appendChild(document.createTextNode(message));
    document.getElementById("ans").appendChild(document.createElement("br"));
}

// Used to detect if a readystate event is dispatched
function catchReadystateEventAbort() {
    log("PASS (readystate event send)");
}

// Called only for readyState == 1
function testAbortWithReadyStateEventLoading()
{
    var xhr;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (ex) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    // We use a slow page to test the LOADING state with the send() flag
    // and we do not catch readyState event before send has been called
    xhr.open("GET", "resources/endlessxml.php", true);
    xhr.send(null);

    ++finishedTests;
    xhr.onreadystatechange = catchReadystateEventAbort;
    xhr.abort();

    if (xhr.readyState == 0)
        log("PASS");
    else
        log("FAILED");

    if (finishedTests == abortToDo.length && window.testRunner)
        testRunner.notifyDone();
}

// Called with num != 1
function testAbortWithReadyStateEvent(num)
{
    var xhr;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (ex) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    xhr.onreadystatechange = function () {
        if (this.readyState == num) {
            ++finishedTests;
            this.onreadystatechange = catchReadystateEventAbort;
            this.abort();
            if (this.readyState == 0)
                log("PASS");
            else
                log("FAILED");
        }

        if (finishedTests == abortToDo.length && window.testRunner)
            testRunner.notifyDone();
    }

    xhr.open("GET", "resources/1251.html", true);
    xhr.send(null);
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var finishedTests = 0;

var abortToDo = ['2', '3', '1'];

for (i = 0; i < abortToDo.length; i++)
    if (abortToDo[i] == 1)
        testAbortWithReadyStateEventLoading();
    else
        testAbortWithReadyStateEvent(abortToDo[i]);

</script>
</body>
</html>
